﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace Test_moduler
{
    public partial class TestForm : Form
    {
        public TestForm()
        {
            InitializeComponent();
        }

        private void buttonChangeBloodLevel_Click(object sender, EventArgs e)
        {
            if (textBoxBloodLevel.Text.Equals(""))
                return;
            try
            {
                Convert.ToInt16(textBoxBloodLevel.Text);
                buttonChangeBloodLevel.Enabled = false;
                while (File.Exists("bloodLevel.param"))
                    Thread.Sleep(1000);

                File.WriteAllText("bloodLevel.param", textBoxBloodLevel.Text);
                textBoxBloodLevel.Text = "";
                while (File.Exists("bloodLevel.param"))
                    Thread.Sleep(1000);
                buttonChangeBloodLevel.Enabled = true;
            }
            catch
            {
                buttonChangeBloodLevel.Enabled = true;
                textBoxBloodLevel.Text = "";
            }
        }

        private void buttonChangePIN_Click(object sender, EventArgs e)
        {
            if (textBoxPIN.Text.Equals(""))
                return;

            try
            {
                int value = Convert.ToInt16(textBoxPIN.Text);
                if (value > 100 || value < 1)
                    throw new Exception();

                buttonChangePIN.Enabled = false;
                while (File.Exists("PIN.param")) 
                    Thread.Sleep(1000);

                File.WriteAllText("PIN.param", textBoxPIN.Text);
                textBoxPIN.Text = "";
                while (File.Exists("PIN.param")) 
                    Thread.Sleep(1000);
                buttonChangePIN.Enabled = true;
            }
            catch
            {
                buttonChangePIN.Enabled = true;
                textBoxPIN.Text = "";
            }
        }

        private void buttonDeliveryFail_Click(object sender, EventArgs e)
        {
            buttonDeliveryFail.Enabled = false;
            while (File.Exists("deliveryFail.param")) 
                Thread.Sleep(1000);

            File.WriteAllText("deliveryFail.param", "");
            while (File.Exists("deliveryFail.param"))
                Thread.Sleep(1000) ;
            buttonDeliveryFail.Enabled = true;
        }

        private void buttonTesterFail_Click(object sender, EventArgs e)
        {
            buttonTesterFail.Enabled = false;
            while (File.Exists("testerFail.param")) 
                Thread.Sleep(1000);

            File.WriteAllText("testerFail.param", "");
            while (File.Exists("testerFail.param")) 
                Thread.Sleep(1000);
            buttonTesterFail.Enabled = true;
        }

        private void buttonChangeNeedle_Click(object sender, EventArgs e)
        {
            if (buttonChangeNeedle.Text.Equals("Remove Needle"))
            {
                buttonChangeNeedle.Enabled = false;
                buttonDeliveryFail.Enabled = false;
                buttonRemoveResevoir.Enabled = false;
                buttonTesterFail.Enabled = false;
                while (File.Exists("needle.param")) 
                    Thread.Sleep(1000);

                File.WriteAllText("needle.param", "false");
                while (File.Exists("needle.param")) 
                    Thread.Sleep(1000);

                buttonChangeNeedle.Enabled = true;
                buttonChangeNeedle.Text = "Insert Needle";
            }
            else
            {
                buttonChangeNeedle.Enabled = false;
                while (File.Exists("needle.param"))
                    Thread.Sleep(1000);

                File.WriteAllText("needle.param", "true");
                while (File.Exists("needle.param"))
                    Thread.Sleep(1000);

                buttonChangeNeedle.Enabled = true;
                buttonChangeNeedle.Text = "Remove Needle";

                buttonDeliveryFail.Enabled = true;
                buttonTesterFail.Enabled = true;
                buttonRemoveResevoir.Enabled = true;
            }
        }

        private void buttonChangeRemainInsulin_Click(object sender, EventArgs e)
        {
            if (textBoxRemainInsulin.Text.Equals(""))
                return;
            try
            {
                int value = Convert.ToInt16(textBoxRemainInsulin.Text);
                if (value > 100 || value < 0)
                    throw new Exception(); 
                buttonChangeRemainInsulin.Enabled = false;
                while (File.Exists("remainInsulin.param"))
                    Thread.Sleep(1000);

                File.WriteAllText("remainInsulin.param", textBoxRemainInsulin.Text);
                textBoxRemainInsulin.Text = "";
                while (File.Exists("remainInsulin.param"))
                    Thread.Sleep(1000);
                buttonChangeRemainInsulin.Enabled = true;
            }
            catch
            {
                buttonChangeRemainInsulin.Enabled = true;
                textBoxRemainInsulin.Text = "";
            }
        }

        private void buttonChangeLifeCirclePump_Click(object sender, EventArgs e)
        {
            try
            {
                int value = Convert.ToInt16(textBoxLifeCirclePump.Text);
                if (value < 1)
                    throw new Exception(); 
                buttonChangeLifeCirclePump.Enabled = false;
                while (File.Exists("lifeCirclePump.param"))
                    Thread.Sleep(1000);

                File.WriteAllText("lifeCirclePump.param", (Convert.ToInt16(textBoxLifeCirclePump.Text) * 1000).ToString());
                textBoxLifeCirclePump.Text = "";
                while (File.Exists("lifeCirclePump.param"))
                    Thread.Sleep(1000);
                buttonChangeLifeCirclePump.Enabled = true;
            }
            catch
            {
                buttonChangeLifeCirclePump.Enabled = true;
                textBoxLifeCirclePump.Text = "";
            }
        }

        private void buttonChangeLifeCircleTester_Click(object sender, EventArgs e)
        {
            try
            {
                int value = Convert.ToInt16(textBoxLifeCircleTester.Text);
                if (value < 1)
                    throw new Exception(); 
                buttonChangeLifeCircleTester.Enabled = false;
                while (File.Exists("lifeCircleTester.param"))
                    Thread.Sleep(1000);

                File.WriteAllText("lifeCircleTester.param", (Convert.ToInt16(textBoxLifeCircleTester.Text)).ToString());
                textBoxLifeCircleTester.Text = "";
                while (File.Exists("lifeCircleTester.param"))
                    Thread.Sleep(1000);
                buttonChangeLifeCircleTester.Enabled = true;
            }
            catch
            {
                buttonChangeLifeCircleTester.Enabled = true;
                textBoxLifeCircleTester.Text = "";
            }
        }

        private void buttonRemoveResevoir_Click(object sender, EventArgs e)
        {
            if (buttonRemoveResevoir.Text.Equals("Remove Insulin Resevoir"))
            {
                buttonRemoveResevoir.Enabled = false;
                buttonDeliveryFail.Enabled = false;
                buttonChangeNeedle.Enabled = false;
                buttonTesterFail.Enabled = false;
                while (File.Exists("insulinResevoir.param"))
                    Thread.Sleep(1000);

                File.WriteAllText("insulinResevoir.param", "false");
                while (File.Exists("insulinResevoir.param"))
                    Thread.Sleep(1000);
                buttonRemoveResevoir.Enabled = true;
                buttonRemoveResevoir.Text = "Insert Insulin Resevoir";
            }
            else
            {
                buttonRemoveResevoir.Enabled = false;
                while (File.Exists("insulinResevoir.param"))
                    Thread.Sleep(1000);

                File.WriteAllText("insulinResevoir.param", "true");
                while (File.Exists("insulinResevoir.param"))
                    Thread.Sleep(1000);

                buttonRemoveResevoir.Enabled = true;
                buttonRemoveResevoir.Text = "Remove Insulin Resevoir";

                buttonDeliveryFail.Enabled = true;
                buttonTesterFail.Enabled = true;
                buttonChangeNeedle.Enabled = true;
            }
        }

        private void TestForm_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.FixedDialog;

        }
    }
}
